﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WareHouse.App
{
    using WareHouse.Contract.Services;
    using WareHouse.Service.Services;

    public partial class FrmLogin : Form
    {
        private IWareHouseService wareHouseService;
        public FrmLogin()
        {
            InitializeComponent();
            wareHouseService=new WareHouseService();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
           
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            var resultLogin = wareHouseService.LoginWithUserNameAndPassword(txtUserName.Text, txtPassword.Text);
            // if true = login succeed
            if (resultLogin != null)
            {
                General.userName = resultLogin.UserName;
                General.groupUserId = resultLogin.GroupUserId;
                General.UserFullName = resultLogin.FullName;
                // check is Admin
                if (resultLogin.IsAdmin)
                {
                    General.isAdmin = true;
                    // is admin
                    this.LoadFormMain();
                    return;
                }
                // is not admin
               this.LoadFormMain();
                return;
            }
            // login failed
            MessageBox.Show("login thất bại");
        }

        private void LoadFormMain()
        {
            if (Application.OpenForms["FrmMain"] == null)
            {
                FrmMain main = new FrmMain();
                main.Show();
                this.Hide();
                return;

            }
            Application.OpenForms["frmItem"].Show();
            this.Hide();
        }

        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtUserName.Text !="" && txtPassword.Text !="")
            {
                if ((Keys)e.KeyChar == Keys.Enter)
                {
                    btnLogin.PerformClick();
                }
            }
            
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
